const ExcelJs = require('exceljs');
const fs = require('fs');
const file = './parameter.xlsx';

async function getParameters() {
    const workbook = new ExcelJs.Workbook()
    await workbook.xlsx.readFile(file);
    const worksheet = workbook.getWorksheet(1);
    const columns = worksheet.columns;
    const len = columns.length;
    const row = worksheet.getRow(1);
    const xml = refactor(row, len);
    fs.writeFileSync('./result.txt', xml);
}

async function getParameters2() {
    const workbook = new ExcelJs.Workbook()
    await workbook.xlsx.readFile(file);
    const worksheet = workbook.getWorksheet(1);
    const len = worksheet.rowCount;
    const xml = refactor2(worksheet, len);
    fs.writeFileSync('./result.txt', xml);
}

const start = '<collectionProp name="Arguments.arguments">\n';
const defaultTap =
    '            <elementProp name="$$key$$" elementType="HTTPArgument">\n' +
    '              <boolProp name="HTTPArgument.always_encode">false</boolProp>\n' +
    '              <stringProp name="Argument.value">${$$key$$}</stringProp>\n' +
    '              <stringProp name="Argument.metadata">=</stringProp>\n' +
    '              <boolProp name="HTTPArgument.use_equals">true</boolProp>\n' +
    '              <stringProp name="Argument.name">$$key$$</stringProp>\n' +
    '            </elementProp>\n';
const end = '          </collectionProp>';

function refactor(row, len) {
    let tap = '';
    let index = 1;
    while (index <= len) {
        tap += defaultTap.replace(/\$\$key\$\$/g, row.getCell(index).value);
        index++;
    }
    return start + tap + end;
}

function refactor2(worksheet, len) {
    let tap = '';
    let index = 1;
    while (index <= len) {
        tap += defaultTap.replace(/\$\$key\$\$/g, worksheet.getRow(index).getCell(1).value);
        index++;
    }
    return start + tap + end;
}

getParameters2().then(() => {
    console.log('success');
})